#include <stdio.h>
#include <stdlib.h>
typedef struct PolyItem {
    int coef;  
    int exp;   
} PolyItem;

typedef struct ListNode {
    PolyItem *item;       
    struct ListNode *next; 
} ListNode;
PolyItem *createPolyItem(int coef, int exp) {
    PolyItem *item = (PolyItem *)malloc(sizeof(PolyItem));
    item->coef = coef;
    item->exp = exp;
    return item;
}
void insertPolyItem(ListNode **head, PolyItem *item) {
    ListNode *node = (ListNode *)malloc(sizeof(ListNode));
    node->item = item;
    node->next = NULL;
    if (*head == NULL) {
        *head = node;
    } else {
        ListNode *p = *head;
        while (p->next != NULL) {
            p = p->next;
        }
        p->next = node;
    }
}
void readPoly(ListNode **head) {
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        int coef, exp;
        scanf("%d%d", &coef, &exp);
        PolyItem *item = createPolyItem(coef, exp);
        insertPolyItem(head, item);
    }
}
void printPoly(ListNode *head) {
    if (head == NULL) {
        printf("0\n");
    } else {
        while (head != NULL) {
            printf("%d %d ", head->item->coef, head->item->exp);
            head = head->next;
        }
        printf("\n");
    }
}
void freePoly(ListNode *head) {
    while (head != NULL) {
        ListNode *p = head;
        head = head->next;
        free(p->item);
        free(p);
    }
}
ListNode *multiplyPoly(ListNode *poly1, ListNode *poly2) {
    ListNode *result = NULL;
    while (poly1 != NULL) {
        ListNode *p = poly2;
        while (p != NULL) {
            int coef = poly1->item->coef * p->item->coef;
            int exp = poly1->item->exp + p->item->exp;
            PolyItem *item = createPolyItem(coef, exp);
            insertPolyItem(&result, item);
            p = p->next;
        }
        poly1 = poly1->next;
    }
    return result;
}

int main() {
    ListNode *poly1 = NULL;
    ListNode *poly2 = NULL;
    readPoly(&poly1);
    readPoly(&poly2);
    ListNode *result = multiplyPoly(poly1, poly2);
    printPoly(result);
    freePoly(poly1);
    freePoly(poly2);
    freePoly(result);
    return 0;
}